
from fastapi import FastAPI,APIRouter,Request
from json.decoder import JSONDecodeError
import sys 
sys.path.append('..')
from utils import homework_record as F 
from utils.http_response import HttpResponse 
from utils.db import engine
router = APIRouter(prefix="/homework_record",tags=["homework_record"])

@router.post("/get_one")
async def get_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.select_by_col(engine,body)
    return resp

@router.post("/update_one")
async def update_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.update_one(engine,body["cond"],body["item"])  
    return resp 

@router.post("/delete_one")
async def delete_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.delete_one(engine,body)  
    return resp 



@router.post("/add_one")
async def add_one(req:Request):
    resp = HttpResponse(code=200,message="success",data=[])
    try:
        body = await req.json()
    except Exception as JSONDecodeError:
        resp.message = "json decode error"
        resp.code = 500
        return resp 
    resp.data = F.add_one(engine,body)  
    return resp 

